#ifndef AUDIOSPEAKER_H
#define AUDIOSPEAKER_H

#include <QMutex>
#include <QQueue>
#include <QThread>
#include <QWaitCondition>

#include <atomic>

class AudioSpeaker : public QThread {
    Q_OBJECT
public:
    explicit AudioSpeaker(QObject *parent = nullptr);
    ~AudioSpeaker();

    void Stop();


    // 内部释放data
    void write(const char *data, int len, double clock);
    double AudioClock();

    // QThread interface
protected:
    struct AudioSpeakerFrame {
        const char *buf;
        int length;
        double clock;
    };

    void run() override;


private:

    long long buffer_size_;

    std::atomic<double> queued_clock_;

    QQueue<AudioSpeakerFrame> queue_;
    QMutex mutex_;
    QWaitCondition cond_;
};

#endif // AUDIOSPEAKER_H
